package a;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSysObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.DfLoginInfo;
import com.documentum.fc.common.IDfLoginInfo;


public class Test3_Documentum {
	private static String user;
	private static String password;
	private static String docbase;


	public static void main(String[] args) throws Exception {
		user = "dmadmin";
		password = "rkk";
		docbase = "ARCH";
		IDfSession session = null;
		try {
			session = getSession();

			String dql = "SELECt r_object_id FROM dm_sysobject WHERE r_content_size >0 AND a_content_type = 'crtext' ENABLE(RETURN_TOP 1)";

			IDfQuery query = new DfQuery();
			query.setDQL(dql);

			String objectId = null;
			IDfCollection coll = null;
			try {
				coll = query.execute(session, IDfQuery.DF_READ_QUERY);
				if(coll.next()) {
					objectId = coll.getString("r_object_id");
				}
			} finally {
				if(coll!=null) coll.close();
			}

			if(objectId!=null) {
				IDfSysObject object = (IDfSysObject) session.getObject(new DfId(objectId));
				ByteArrayInputStream input = object.getContent();
				ByteArrayOutputStream output = new ByteArrayOutputStream();
				
				byte[] bytes = new byte[input.available()];
				input.read(bytes);
				System.out.print(new String(bytes));
			}
		}catch (Exception e) {
				e.printStackTrace();			
		} finally {
			if(session !=null) session.disconnect();
		}
	}

	private static IDfSession getSession() throws Exception{
		IDfClientX cx = new DfClientX();
		IDfClient client = cx.getLocalClient();
		IDfLoginInfo login = new DfLoginInfo();
		login.setUser(user);
		login.setPassword(password);
		return client.newSession(docbase, login);
	}
	
	private static void createSign(IDfSession session, String documentId, byte[] signature) throws DfException, Exception {
		IDfSysObject sign ;
		sign = getSignByDocumentId(session, documentId);
		if (null == sign) {
			sign = (IDfSysObject) session.newObject("ddt_sign");
			sign.setObjectName(documentId + "_signature");
			sign.setString("dsid_document", documentId);
			sign.link("/Office Utils/Signatures");
			sign.setContentType("crtext"); 
		}

		ByteArrayOutputStream output = new ByteArrayOutputStream();
		output.write(signature);
		sign.setContent(output);
		
		sign.save();
	}
	
	private static IDfSysObject getSignByDocumentId(IDfSession session, String documentId) throws DfException {
		String dql = "ddt_sign WHERE dsid_document = '" + documentId + "'";
		return (IDfSysObject) session.getObjectByQualification(dql);
	}
}
